package org.grottarossa.template.data.pojo.workflows;


import java.util.ArrayList;
import java.util.Hashtable;

import org.grottarossa.template.drivers.components.tokens.WorkFlowId;

public class WorkflowList extends ArrayList<Workflow>
{

	private static final long serialVersionUID = -1498418080737491356L;

	public Workflow getWorkflow(WorkFlowId iWorkflowId, int iWorkflowVersion)
	{

		for ( int i=0; i<this.size(); i++ ) {
			Workflow workflow = this.get(i);

			if ( ( workflow.getWorkflowId().equals( iWorkflowId )) && (workflow.getWorkflowVersion()==iWorkflowVersion) )
				return workflow;
		}

		return null;
	}

	/***
	 * Restituisce la lista filtrata sulle ultime versioni dei workflow
	 * @return
	 */
	public WorkflowList getOnlyLatestVersions()
	{
		WorkflowList latestVersions = new WorkflowList();
		Hashtable<String, Workflow> latestVarsionsTable = new Hashtable<String, Workflow>();

		for(Workflow w : this)
		{
			if(!latestVarsionsTable.containsKey(w.getWorkflowName()) ||
					latestVarsionsTable.get(w.getWorkflowName()).getWorkflowVersion()
					< w.getWorkflowVersion())
				latestVarsionsTable.put(w.getWorkflowName(), w);
		}

		latestVersions.addAll(latestVarsionsTable.values());

		return latestVersions;
	}

}
